package com.xjh.t.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.*;
import com.xjh.t.domain.Permission;
import com.xjh.t.domain.Role;
import com.xjh.t.domain.User;
import com.xjh.t.service.IPermissionService;
import com.xjh.t.service.IRoleService;
import com.xjh.t.vo.PermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("loadIndexLeftMenuJson")
    public DataGridView loadIndexLeftMenuJson(PermissionVo vo)
    {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constast.TYPE_MENU);
        queryWrapper.eq("available", Constast.TYPE_AVAILABLE_TRUE);

        User user =  (User)WebUtils.getSession().getAttribute("user");
        List<Permission> list = null;
        if(user.getType() == Constast.USER_TYPE_SUPER )
        {
            list = permissionService.list(queryWrapper);
        }
        else
        {
           // QueryWrapper<Role> queryWrapperRole=new QueryWrapper<>();
          //  queryWrapperRole.eq("available", Constast.TYPE_AVAILABLE_TRUE);
          //  List<Map<String, Object>> listMaps = this.roleService.listMaps(queryWrapperRole);

            //2,查询当前用户拥有的角色ID集合
            List<Integer> currentUserRoleIds=this.roleService.queryUserRoleIdsByUid(user.getId());
            List<Integer> allPermissions = new ArrayList<>();
            for(Integer roleid:currentUserRoleIds)
            {
                List<Integer> permissions = this.roleService.queryRolePermissionIdsByRid(roleid);
                for(Integer permissonid:permissions) {
                    if (allPermissions.indexOf(permissonid) <= 0)
                        allPermissions.add(permissonid);
                }
            }
            queryWrapper.in( "id", allPermissions);

            list = permissionService.list(queryWrapper);
        }

        List<TreeNode> treeNodes = new ArrayList<>();
        for(Permission permission:list)
        {
            treeNodes.add(new TreeNode(permission.getId(),permission.getPid(),permission.getTitle(),permission.getIcon(),permission.getHref(),permission.getOpen()==Constast.OPEN_TRUE));
        }

        return new DataGridView(TreeNodeBuilder.build(treeNodes,1));
    }
    
    //菜单管理开始
    @RequestMapping("loadMenuManagerLeftJson")
    public DataGridView loadMenuManagerLeftJson(PermissionVo vo)
    {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",Constast.TYPE_MENU);
        List<Permission> list = this.permissionService.list(queryWrapper);
        List<TreeNode> treeNodes = new ArrayList<>();
        for(Permission permission:list)
        {
            treeNodes.add(new TreeNode(permission.getId(),permission.getPid(),permission.getTitle(),permission.getOpen() == Constast.OPEN_TRUE));
        }

        return new DataGridView(treeNodes);
    }

    @RequestMapping("loadAllMenu")
    public DataGridView loadAllMenu(PermissionVo vo)
    {
        IPage<Permission> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(vo.getId() != null,"id",vo.getId()).or().eq(vo.getId() != null,"pid",vo.getId());
        queryWrapper.eq("type",Constast.TYPE_MENU);
        queryWrapper.like(StringUtils.isNotBlank(vo.getTitle()),"title",vo.getTitle());
        queryWrapper.orderByAsc("ordernum");
        this.permissionService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @RequestMapping("addMenu")
    public ResultObj addMenu(PermissionVo vo)
    {
        try
        {
            vo.setType(Constast.TYPE_MENU);
            this.permissionService.save(vo);
            return ResultObj.ADD_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.ADD_ERROR;
        }
    }

    @RequestMapping("loadMenuMaxOrderNum")
    public Map<String,Object> loadMenuMaxOrderNum()
    {
        Map<String,Object> map = new HashMap<String,Object>();
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<Permission> list = this.permissionService.list(queryWrapper);
        if(list.size() > 0)
        {
            map.put("value",list.get(0).getOrdernum() + 1);
        }
        else
            map.put("value",1);
        return map;
    }

    @RequestMapping("updateMenu")
    public ResultObj updateMenu(PermissionVo vo)
    {
        try
        {
            this.permissionService.updateById(vo);
            return ResultObj.UPDATE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.UPDATE_ERROR;
        }

    }
    @RequestMapping("checkMenuHasChildrenNode")
    public Map<String,Object> checkMenuHasChildrenNode(PermissionVo vo)
    {
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(vo.getId() != null,"pid",vo.getId());
        List<Permission> list = this.permissionService.list(queryWrapper);
        if(list.size() > 0)
        {
            map.put("value",true);
        }
        else
            map.put("value",false);
        return map;
    }

    @RequestMapping("deleteMenu")
    public ResultObj deleteMenu(Integer id)
    {
        try
        {
            this.permissionService.removeById(id);
            return ResultObj.DELETE_SUCESS;
        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.DELETE_ERROR;
        }

    }
    //菜单管理结束
}
